﻿//Copyright (c) Microsoft Corporation. All rights reserved.

using Microsoft.Ted.Wacel.Cache;
using System;

namespace Microsoft.Ted.Wacel.TestConsole
{
    public class CachedCircularBufferTester
    {
        public static void ReadWriteArrayItem()
        {
            CircularBuffer<int> buffer = new CircularBuffer<int>("my_buffer", 5);
            buffer.Add(1234);
            int value = buffer[0];
            VisualTester.AreEqual(1234, buffer[0]);
        }
        public static void BufferWrap()
        {
            CircularBuffer<int> buffer = new CircularBuffer<int>("my_buffer", 5);
            buffer.Add(1);
            buffer.Add(2);
            buffer.Add(3);
            buffer.Add(4);
            buffer.Add(5);
            buffer.Add(6);
            VisualTester.AreEqual(6, buffer[0]);
        }
        public static void BufferHistory()
        {
            CircularBuffer<int> buffer = new CircularBuffer<int>("my_buffer", 5);
            buffer.Add(1);
            buffer.Add(2);
            buffer.Add(3);
            buffer.Add(4);
            buffer.Add(5);
            VisualTester.AreEqual(3, buffer[-2]);
        }
        public static void BufferHistoryAfterWrap()
        {
            CircularBuffer<int> buffer = new CircularBuffer<int>("my_buffer", 5);
            buffer.Add(1);
            buffer.Add(2);
            buffer.Add(3);
            buffer.Add(4);
            buffer.Add(5);
            buffer.Add(6);
            buffer.Add(7);
            buffer.Add(8);
            VisualTester.AreEqual(4, buffer[-4]);
        }
        public static void BufferOutOfIndex()
        {
            CircularBuffer<int> buffer = new CircularBuffer<int>("my_buffer", 5);
            try
            {
                int value = buffer[-5];
                VisualTester.Assert(false);
            }
            catch (IndexOutOfRangeException)
            {
                VisualTester.Assert(true);
            }
            catch (Exception exp)
            {
                VisualTester.Assert(false, exp.Message);
            }
        }
    }
}
